package functions;

import utils.Utility;
import constants.Constants;
import domain.BioRecord;
import domain.DelRecord;
import domain.EstRecord;
import domain.FutRecord;
import domain.InsRecord;
import domain.MkvRecord;
import domain.PolRecord;
import domain.RpoRecord;

public class InitFunctions {
	
	

	
	/******************************************************************************
	FREE_BIO_RECORD
	******************************************************************************/
	public static BioRecord free_bio_record(BioRecord bio)
	{
		bio = new BioRecord();
		return bio;
	}/*** end FREE_BIO_RECORD ***/
	
	
	
	
	
	/******************************************************************************
	FREE_DEL_RECORD
	******************************************************************************/
	public static DelRecord free_del_record(DelRecord del)
	{
		del = new DelRecord();
		return del;
	}/*** end FREE_DEL_RECORD ***/
	
	
	
	
	/******************************************************************************
	FREE_EST_RECORD
	******************************************************************************/
	public static EstRecord free_est_record(EstRecord est)
	{
		est = new EstRecord();
		return est;
	}/*** end FREE_EST_RECORD ***/
	
	
	
	/******************************************************************************
	FREE_FUT_RECORD
	******************************************************************************/
	public static FutRecord free_fut_record(FutRecord fut)
	{
		fut = new FutRecord();
		return fut;
	}/*** end FREE_FUT_RECORD ***/
	
	
	/******************************************************************************
	FREE_INS_RECORD
	******************************************************************************/
	public static InsRecord free_ins_record(InsRecord ins)
	{
		ins = new InsRecord();
		return ins;
	}/*** end FREE_INS_RECORD ***/
	
	
	
	/******************************************************************************
	FREE_MKV_RECORD
	******************************************************************************/
	public static MkvRecord free_mkv_record(MkvRecord mkv)
	{
		mkv = new MkvRecord();
		return mkv;
	}/*** end FREE_MKV_RECORD ***/
	
	
	
	
	/******************************************************************************
	FREE_POL_RECORD
	******************************************************************************/
	public static PolRecord free_pol_record(PolRecord pol)
	{
		pol = new PolRecord();
		return pol;
	}/*** end FREE_POL_RECORD ***/
	
	
	/******************************************************************************
	FREE_RPO_RECORD
	******************************************************************************/
	public static RpoRecord free_rpo_record(RpoRecord rpo)
	{
		rpo = new RpoRecord();
		return rpo;
	}/*** end FREE_RPO_RECORD ***/
	
	
	
	
	
	
	/******************************************************************************
	INIT_BIO_RECORD: Initializes bio_record for RPO calculations.
	******************************************************************************/
	public static BioRecord init_bio_record(BioRecord bio)
	{
		return reset_bio_record(bio);
	}/*** end INIT_BIO_RECORD ***/
	
	
	/******************************************************************************
	INIT_DEL_RECORD: Initializes del_record.
	******************************************************************************/
	public static DelRecord init_del_record(DelRecord del)
	{
		return reset_del_record(del);
	}/*** end INIT_DEL_RECORD ***/
	
	
	/******************************************************************************
	INIT_EST_RECORD: Initializes est_record for estimated milk yields.
	******************************************************************************/
	public static EstRecord init_est_record(EstRecord est)
	{
		return reset_est_record(est);
	}/*** end INIT_EST_RECORD ***/
	
	
	/******************************************************************************
	INIT_FUT_RECORD: Initializes fut_record for RPO calculations.
	******************************************************************************/
	public static FutRecord init_fut_record(FutRecord fut)
	{
		return reset_fut_record(fut);
	}/*** end INIT_FUT_RECORD ***/
	
	
	/******************************************************************************
	INIT_INS_RECORD: Initializes ins_record for Insemination value calculations.
	******************************************************************************/
	public static InsRecord init_ins_record(InsRecord ins)
	{
		return reset_ins_record(ins);
	}/*** end INIT_INS_RECORD ***/
	
	
	/******************************************************************************
	INIT_MKV_RECORD: Initializes mkv_record for Markov chain calculations for t >= MAXU.
	******************************************************************************/
	public static MkvRecord init_mkv_record(MkvRecord mkv)
	{
		//	reset_mkv_record(2,MAXM);		//reset entire record
		//to save time, do not reset here. mkv_record is often reset in markov_chain()
		return mkv;
	}/*** end INIT_MKV_RECORD ***/
	
	
	/******************************************************************************
	INIT_POL_RECORD: Initializes pol_record for RPO calculations.
	******************************************************************************/
	public static PolRecord init_pol_record(PolRecord pol)
	{
		return reset_pol_record(pol);
	}/*** end INIT_POL_RECORD ***/
	
	
	
	
	
	
	/******************************************************************************
	INIT_RPO_RECORD: Initializes rpo_record for RPO calculations.
	******************************************************************************/
	public static RpoRecord init_rpo_record(RpoRecord rpo)
	{
		return reset_rpo_record(rpo);
	}/*** end INIT_RPO_RECORD ***/
	
	
	
	
	
	
	
	
	
	
	
	
	/******************************************************************************
	RESET_BIO_RECORD:
	******************************************************************************/
	public static BioRecord reset_bio_record(BioRecord bio)
	{
		int	curr = 0, lact = 0, mim = 0, mpreg = 0, sea = 0, next = 0;
	
		System.out.println("\n RESET_BIO_RECORD started ...");
		Utility.timetaken(3);
		Utility.write_msg_file(30,0);
	
		for(mpreg = 0; mpreg <= Constants.MPREG; mpreg++) bio.getAbort()[mpreg] = 0.0;
		bio.setAnalysis(0);
		for(mpreg = 0; mpreg <= Constants.MPREG; mpreg++) bio.getBwpreg()[1][mpreg] = bio.getBwpreg()[2][mpreg] = 0.0;
		bio.setConcabs(0.0);
		bio.setConcseapct(0.0);
		bio.setCullabs(0.0);
		bio.setCullmargin(0.0);
		bio.setCullmilk(0.0);
		bio.setCullpct(0.0);
		bio.setCullseapct(0.0);
		bio.setDmi_dry(0.0);
		bio.setFunk(0);
		bio.setHeatabs(0.0);
		bio.setHeatseapct(0.0);
		bio.setInvcullpct(0.0);
		bio.setInterest(0.0);
		bio.setMaxlact(0);
		bio.setLbm(0);
		bio.setMaxmim(0);
		for(mpreg = 0; mpreg <= Constants.MPREG; mpreg++) bio.getMilk_mpreg()[mpreg] = 0.0;
		bio.getMilkabs()[0] = bio.getMilkabs()[1] = bio.getMilkabs()[2] = bio.getMilkabs()[3] = 0.0;
		bio.getMilkpct()[0] = bio.getMilkpct()[1] = bio.getMilkpct()[2] = bio.getMilkpct()[3] = 0.0;
		bio.setMilkrepeat(0.0);
		bio.setMilkseapct(0.0);	
		bio.setP_calf(0.0);
		bio.setP_fixed_labor(0.0);
		bio.setP_fixed_other(0.0);
		bio.setP_labor(0.0);
		bio.setP_loss(0.0);
		bio.setP_other_dry(0.0);
		bio.setP_other_lact(0.0);
		bio.setP_semen(0.0);
		bio.setP_tmrdmdry(0.0);			
		bio.setP_tmrdmlac(0.0);			
		bio.setPa_cv(0.0);
		bio.setPctfat(0.0);
		bio.setPolicy(0);
		bio.setStart_sea(0);
		bio.setTimefresh(0.0);
		bio.setTimelact(0.0);
		bio.setTimedry(0.0);
		bio.setTimebreeding(0.0);
		bio.setUnit(1);			//kg
		for(lact = 0; lact <= 3; lact++) bio.getVwp()[lact] = 0;
		bio.setWriterpo(0);
		bio.setWritepol(0);
		for(sea = 0; sea <= Constants.SEA; sea++) 
		{
			bio.getConcsea()[sea] = 0.0;
			bio.getHeatsea()[sea] = 0.0;
			for(mim = 0; mim <= Constants.MIM; mim++) 
			{
			//	bio.gettimeheat[mim] = 0.0;
				bio.getHeatdetectionrate()[mim][sea] = 0.0;
				for(lact = 0; lact <= Constants.LACT; lact++) bio.getConceptionrate()[lact][mim][sea] = 0.0;
				bio.getReprocost()[mim][sea] = 0.0;
			}
		}
		for(sea = 0; sea <= 2*Constants.SEA; sea++) 
		{
			bio.getEnter()[sea] = 0;
			bio.getP_heifer()[sea] = 0.0;
			bio.getP_milk()[sea] = 0.0;
			bio.getServe()[sea] = 0;
			bio.getP_bodyweight()[sea] = 0.0;
		}
		for(mim = 0; mim <= Constants.MIM; mim++) bio.getVetcost()[mim] = 0.0;
		for(mim = 0; mim <= Constants.MIM; mim++) bio.getConcmim()[mim] = 0.0;
		for(curr = 0; curr <= Constants.CURR; curr++)
		{
			bio.getSmy()[curr] = 0.0;
			for(lact = 0; lact <= Constants.LACT; lact++)
			{
				for(sea = 0; sea <= Constants.SEA; sea++) bio.getM305()[curr][lact][sea] = 0.0;
				for(mim = 0; mim <= Constants.MIM; mim++)
				{
					for(sea = 0; sea <= Constants.SEA; sea++) bio.getHi()[curr][lact][mim][sea] = 0;
					for(sea = 0; sea <= Constants.SEA; sea++) bio.getLo()[curr][lact][mim][sea] = 0;
					for(sea = 0; sea <= Constants.SEA; sea++) for(next = 0; next <= Constants.CURR; next++) bio.getPnext()[curr][lact][mim][sea][next] = 0.0;
					for(mpreg = 0; mpreg <= Constants.MPREG; mpreg++)
					for(sea = 0; sea <= Constants.SEA; sea++)
					{
	//System.out.println("\n i:%d j:%d k:%d m:%d",i,j,k,m);
						bio.getRev()[curr][lact][mim][mpreg][sea] = 0.0;			//revenue
						bio.getRev()[curr][lact][mim][mpreg][sea+12] = 0.0;			//revenue
						if(mpreg == 0) bio.getSel()[lact][mim][sea] = 0.0;			//sell
						if(mpreg == 0) bio.getSel()[lact][mim][sea+12] = 0.0;		//sell
						bio.getIofc()[curr][lact][mim][mpreg][sea] = 0.0;			//income over feed cost
						bio.getIofc()[curr][lact][mim][mpreg][sea+12] = 0.0;		//income over feed cost
						bio.getMilkyield()[curr][lact][mim][mpreg][sea] = 0.0;		//milk yield
						bio.getDmi()[curr][lact][mim][mpreg][sea] = 0.0;			//dmi
						bio.getTimecows()[curr][lact][mim][mpreg][sea] = 0.0;		//time
						bio.getVarcost()[curr][lact][mim][mpreg][sea] = 0.0;		//variable cost (feed, vet, labor, other var)
						bio.getVarcost()[curr][lact][mim][mpreg][sea+12] = 0.0;		//variable cost (feed, vet, labor, other var)
					}
				}
			}
		}
		for(lact = 0; lact <= Constants.LACT; lact++) 
			for(mim = 0; mim <= Constants.MIM; mim++)
			{
				for(mpreg = 0; mpreg <= Constants.MPREG; mpreg++) 
					bio.getBw()[lact][mim][mpreg] = 0.0;				//bodyweight
				for(sea = 0; sea <= Constants.SEA; sea++)	
					bio.getPind()[lact][mim][sea] = 0.0;				//probability of involuntary culling
			}
		System.out.println("\n RESET_BIO_RECORD done ...");
		return bio;
	}/*** end RESET_BIO_RECORD ***/
	
	
	
	
	
	/******************************************************************************
	RESET_DEL_RECORD: Resets the DEL record
	******************************************************************************/
	public static DelRecord reset_del_record(DelRecord del)
	{
		int	i = 0, j = 0, k = 0, m = 0, n = 0;
	
		System.out.println("\n RESET_DEL_RECORD started ...");
		for(i = 0; i <= Constants.CURR; i++)
			for(j = 0; j <= Constants.LACT; j++)
				for(k = 0; k <= Constants.MIM; k++)
					for(n = 0; n <= 2*Constants.SEA; n++)
					{
						for(m = 0; m <= Constants.MIM; m++)
						{
							del.getOpen()[i][j][k][m][n] = 0.0; 
							del.getPreg()[i][j][k][m][n] = 0.0;
						}
					}
		System.out.println("\n RESET_DEL_RECORD done ...");
		return del;
	}/*** end RESET_DEL_RECORD ***/
	
	
	
	
	/******************************************************************************
	RESET_EST_RECORD: Resets the est record which holds the predicted milk yields.
	******************************************************************************/
	public static EstRecord reset_est_record(EstRecord est)
	{
		int	i = 0, j = 0, k = 0, m = 0, n = 0;
	
		System.out.println("\n RESET_FUT_RECORD started ... ");
		for(i = 0; i <= Constants.CURR; i++)
			for(j = 0; j <= 3; j++)
				for(k = 0; k <= Constants.MIM; k++)
					for(m = 0; m <= Constants.SEA; m++)
						for(n = 0; n <= Constants.MIM; n++)
							est.getMlk()[i][j][k][m][n] = 0.0;
		for(i = 0; i <= Constants.CURR; i++)
			for(j = 0; j <= 3; j++)
				for(k = 0; k <= Constants.MIM; k++)
					for(m = 0; m <= Constants.MIM; m++)
						for(n = 0; n <= Constants.CURR; n++)
							est.getProb()[i][j][k][m][n] = 0.0;
		System.out.println("\n RESET_EST_RECORD done ...");
		return est;
	}/*** end RESET_EST_RECORD ***/
	
	
	
	/******************************************************************************
	RESET_FUT_RECORD: Resets the fut record. When userpo == 1, then 
	  the run can be short and still an optimal policy can be calculated, unaffected 
	  by the decision at the end of planning horizon.
	******************************************************************************/
	public static FutRecord reset_fut_record(FutRecord fut)	
	{
		int futfile = 0;
		int	i = 0, j = 0, k = 0, m = 0, n = 0, ok = 0;
	
		System.out.println("\n RESET_FUT_RECORD started ... ");
	
		fut.setHeifer0(0.0);
		for(i = 0; i <= Constants.SEA*2; i++) fut.getCash()[i] = 0.0;
		for(n = 0; n <= 2; n++) fut.getEnter()[n] = 0.0;
		for(i = 0; i <= Constants.CURR; i++)
			for(j = 0; j <= Constants.LACT; j++)
				for(k = 0; k <= Constants.MIM; k++)
				{
					for(m = 0; m <= Constants.MPREG; m++) for(n = 0; n <= 2; n++) fut.getCow()[i][j][k][m][n] = 0.0;
					for(m = 0; m <= Constants.MIM; m++) for(n = 0; n <= 2; n++) fut.getOpen()[i][j][k][m][n] = 0.0;
					for(m = 0; m <= Constants.MIM; m++) for(n = 0; n <= 2; n++) fut.getPreg()[i][j][k][m][n] = 0.0;
				}
		if(futfile == 1) ok = Utility.read_warm_file();	//reads warm.txt file()
		if(ok == 1)
		{
			fut.getEnter()[0] = fut.getEnter()[2];
			for(i = 1; i <= Constants.CURR; i++)
				for(j = 1; j <= Constants.LACT; j++)
					for(k = 1; k <= Constants.MIM; k++)
						for(m = 0; m <= Constants.MPREG; m++) if(m == 0 || (k - m >= 2 && k - m <= 15))
						{
							fut.getCow()[i][j][k][m][0] = fut.getCow()[i][j][k][m][2];// - fut.getheifer0; 
						//	System.out.println("\n %d %d %d %d %f",i,j,k,m,fut.getcow[i][j][k][m][0]);
						}
		//	fut.getheifer0 = 0.0;
		}
		System.out.println("\n RESET_FUT_RECORD done ...");
	//	if(ok == 1) System.out.println(" fut.txt read");
		return fut;
	}/*** end RESET_FUT_RECORD ***/
	
	
	
	/******************************************************************************
	RESET_INS_RECORD: Resets the INS record (Insemination Value = SERV - KEEP)
	******************************************************************************/
	public static InsRecord reset_ins_record(InsRecord ins)
	{
		int	i = 0, j = 0, k = 0, m = 0;
	
		System.out.println("\n RESET_INS_RECORD started ...");
		for(i = 0; i <= Constants.CURR; i++)
			for(j = 0; j <= Constants.LACT; j++)
				for(k = 0; k <= Constants.MIM; k++)
					for(m = 0; m <= 2*Constants.SEA; m++)
						ins.getCow()[i][j][k][m] = 0.0; 
		System.out.println("\n RESET_INS_RECORD done ...");
		return ins;
	}/*** end RESET_INS_RECORD ***/
	
	
	
	/******************************************************************************
	RESET_MKV_RECORD: Reset all or part of markov chain.
	- Resetting all of mkv.get may take a long time!
	******************************************************************************/
	public static MkvRecord reset_mkv_record(MkvRecord mkv, int what, int maxstages)		//n == ii or jj or 2 or 3 in MARKOV_CHAIN()
	{
		int	i = 0, j = 0, k = 0, l = 0, m = 0, n = 0, t = 0, 
						firstn = 0, lastn = 0;
	
		if(what == 3) System.out.println("\n RESET_MKV_RECORD started "+what+" ...");
	//	write_msg_file(20,0);
		switch(what)
		{
			//reset cow+heifer array
			case 0:	firstn = 0; lastn = 0; break;	//reset after every stage
			case 1:	firstn = 1; lastn = 1; break;	//reset after every stage
		//	case 2:	firstn = 0; lastn = 2; break;	//reset all
			case 3:	firstn = 0; lastn = 1; break;	//reset all, before stage 1
			case 4: firstn = 0; lastn = 2; break;	//reset all cow arrays (in read_mkv_record);
			default: Utility.stop(1);
		}
	
		for(n = firstn; n <= lastn; n++) 
		{
	//		if(timetaken(4) >= 4)		//write message every 4 seconds
	//		{
	//			write_msg_file(20,(double)n/(double)lastn*100);
	//			timetaken(3);
	//		}
	
			for(i = 0; i <= Constants.CURR; i++)
				for(j = 0; j <= Constants.LACT; j++)
					for(k = 0; k <= Constants.MIM; k++)
						for(m = 0; m <= Constants.MPREG; m++)
						{
							mkv.getCow()[i][j][k][m][n] = 0.0;
							if(m == 0) mkv.getFs()[i][j][k][n] = 0.0;
							if(m == 0) mkv.getVw()[i][j][k][n] = 0.0;
						}
			for(i = 0; i <= Constants.AGE; i++) 
				for(m = 0; m <= Constants.MPREG; m++)
					mkv.getHeifer()[i][m][n] = 0.0;
	
			for(i = 0; i <= maxstages; i++)				//mkv.getkeep[][] is used when 'all == 0' in markov_chain() to determine mkv.getheiferpool
				for(j = 0; j <= Constants.CURR; j++)
					mkv.getKeep()[j][i] = 0.0;
		}
		if(what == 0 || what == 1 || what == 4) return mkv;
	
	//****reset remainder of MKV record****
		Utility.timetaken(3);
		for(t = 0; t <= maxstages; t++)
		{
			mkv.getAbortions()[t] = 0.0;
			mkv.getAbortions_cnt()[t] = 0.0;
			mkv.getBreedings1()[t] = 0.0;
			mkv.getBreedingsupplycost()[t] = 0.0;		//drugs + semen
			mkv.getBreedinglaborcost()[t] = 0.0;		//labor for heat detection + insemination	
			mkv.getCalves_born()[t] = 0.0;
			mkv.getCalved()[t] = 0.0;
			mkv.getCalves_born()[t] = 0.0;
			mkv.getCash()[t] = 0.0;
			mkv.setCash_maxm(0.0);
			mkv.getCalfsales()[t] = 0.0;
			mkv.getCowsales()[t] = 0.0;
			mkv.getCullprice()[t] = 0.0;
			mkv.getCwt()[t] = 0.0;
			mkv.getDim()[t] = 0.0;
			mkv.getDim_()[t] = 0.0;
			if(t<mkv.getDimy().length) mkv.getDimy()[t] = 0.0;
	
			for(i = 0; i <= Constants.MIM; i++) mkv.getDmi()[i][t] = 0.0;
			for(i = 0; i <= Constants.MIM; i++) mkv.getDmi_cnt()[i][t] = 0.0;
			mkv.getDryoffyield()[t] = 0.0;
			mkv.getDryoffyield_cnt()[t] = 0.0;
			mkv.getDysopen()[t] = 0.0;
			for(i = 0; i <= Constants.MIM; i++) mkv.getEligible_bred()[i][t] = 0.0;
			for(i = 0; i <= Constants.MIM; i++) mkv.getEligible_keep()[i][t] = 0.0;
			for(i = 0; i <= Constants.MIM; i++) mkv.getEligible_preg()[i][t] = 0.0;
			for(i = 0; i <= Constants.MIM; i++) mkv.getEligible_serv()[i][t] = 0.0;
			mkv.getEnter()[t] = 0;
			mkv.getFcalfcost()[t] = 0.0;
			mkv.getFeedcost()[0][t] = mkv.getFeedcost()[1][t] = 0.0;
			mkv.setFinish_sea(0);
			mkv.setFitness(0.0);
			mkv.getFixedcost_labor()[t] = 0.0;
			mkv.getFixedcost_other()[t] = 0.0;
			mkv.getHeifers_purchased()[t] = 0.0;
			mkv.getHeiferpurchasecost()[t] = 0.0;
			mkv.getHeifers_raised()[t] = 0.0;
			mkv.getHeifers_sold()[t] = 0.0;
			if(what == 2) mkv.getHeiferpool()[t] = 0.0;
			for(i = 0; i <= Constants.CURR; i++) mkv.getKeep()[i][t] = 0.0;
			mkv.getKeep_rev0()[t] = 0.0;
			mkv.getLaborcost()[t] = 0.0;
			mkv.getLabortime()[t] = 0.0;
			for(i = 0; i <= Constants.LACT; i++) mkv.getLact()[i][t] = 0.0;
			mkv.getLoss()[t] = 0.0;
			for(i = 0; i <= Constants.MIM; i++) mkv.getMilkyield()[i][t] = 0.0;
			for(i = 0; i <= Constants.MIM; i++) mkv.getMilkyield_cnt()[i][t] = 0.0;
			mkv.getMilksales()[t] = 0.0;
			mkv.getMilking()[t] = 0.0;
			mkv.getMilksales()[t] = 0.0;
			for(i = 0; i <= Constants.CURR; i++) for(j = 0; j <= Constants.LACT; j++) mkv.getNewpreg()[i][j][t] = 0.0;
			for(i = 0; i <= Constants.CURR; i++) mkv.getOpen()[i][t] = 0.0;
			mkv.getOthercost()[t] = 0.0;
			mkv.setPenalty(0.0);
			for(i = 0; i <= Constants.CURR; i++) mkv.getPind()[i][t] = 0.0;
			for(i = 0; i <= Constants.CURR; i++) mkv.getPreg()[i][t] = 0.0;
			for(i = 0; i <= Constants.CURR; i++) mkv.getPresent()[i][t] = 0.0;
			for(i = 0; i <= Constants.MPREG+1; i++) mkv.getPv_t()[t][i] = mkv.getPv_t_cnt()[t][i] = 0.0;
			for(i = 0; i <= Constants.CURR; i++) mkv.getRepl()[i][t] = 0.0;
			mkv.getRev()[t] = 0.0;
			mkv.getRpo()[t] = 0.0;
			mkv.getRpo_cnt()[t] = 0.0;
			if(t <= Constants.MAXU) mkv.getT()[t] = 0.0;
			mkv.getSmy()[t] = 0.0;
			mkv.setStart_cows(0);
			mkv.setStart_day(0);
			mkv.setStart_sea(0);
			mkv.getTotrevenue()[t] = 0.0;
			mkv.getTotcost()[t] = 0.0;
			mkv.getValue()[t] = 0.0;
			mkv.getValue_cnt()[t] = 0.0;
			mkv.getVetcost()[t] = 0.0;
	//		mkv.getvwp[t] = 0.0;
	//		mkv.getvwp_cnt[t] = 0.0;
	
			for(i = 0; i <= Constants.MIM; i++) 
			{
				if(t == 0 && (i == 0 || i == 5 || i == 10 || i == 15 || i ==20 || i == Constants.MIM)) 
				{
					System.out.println((double)i/Constants.MIM*40);			//apparently the first i takes more time than the other i's
					Utility.write_msg_file(20,(double)i/(double)Constants.MIM*40);
				}
	//&
				mkv.getRepl_mim()[i][t] = 0.0;
				mkv.getPind_mim()[i][t] = 0.0;
				mkv.getInsval()[i][t] = 0.0;
				//resetting mkv.getpregval[] takes a long time: minize need for resetting, or break down in smaller arrays
				for(k = 0; k <= Constants.CURR; k++) for(l = 0; l <= Constants.LACT; l++) for(j = 0; j <= Constants.MPREG; j++) mkv.getPregval()[k][l][i][j][t] = 0.0;
				for(k = 0; k <= Constants.CURR; k++) for(l = 0; l <= Constants.LACT; l++) for(j = 0; j <= Constants.MPREG; j++) mkv.getPregval_cnt()[k][l][i][j][t] = 0.0;
	//&
				for(l = 0; l <= Constants.LACT; l++) mkv.getClvinterval()[l][i][t] = 0.0;
				for(j = 0; j <= Constants.CURR; j++) for(l = 0; l <= Constants.LACT; l++) mkv.getDaystoconc()[j][l][i][t] = 0.0;
				for(j = 0; j <= Constants.CURR; j++) for(l = 0; l <= Constants.LACT; l++) mkv.getDvwp()[j][l][i][t] = 0.0;
				for(j = 0; j <= Constants.CURR; j++) for(l = 0; l <= Constants.LACT; l++) mkv.getDfs()[j][l][i][t] = 0.0;
				for(j = 0; j <= Constants.CURR; j++) for(l = 0; l <= Constants.LACT; l++) mkv.getDlbm()[j][l][i][t] = 0.0;
	
	
				for(j = 0; j <= Constants.CURR; j++) mkv.getRepl_open_cnt()[j][i][t] = 0.0;	//vol. culling open cows: probability
				for(j = 0; j <= Constants.CURR; j++) mkv.getRepl_open_iof()[j][i][t] = 0.0;	//vol. culling open cows: income over feed cost
				for(j = 0; j <= Constants.CURR; j++) mkv.getRepl_open_mlk()[j][i][t] = 0.0;	//vol. culling open cows: milk yield
				for(j = 0; j <= Constants.CURR; j++) mkv.getRepl_open_rev()[j][i][t] = 0.0;	//vol. culling open cows: margin
				for(j = 0; j <= Constants.CURR; j++) mkv.getRepl_open_rpo()[j][i][t] = 0.0;	//vol. culling open cows: rpo
				for(j = 0; j <= Constants.CURR; j++) mkv.getDysopencost()[j][i][t] = 0.0;		//cost of day later pregnant
				for(j = 0; j <= Constants.CURR; j++) mkv.getDysopencost_cnt()[j][i][t] = 0.0;	//cost of day later pregnant counter
				for(j = 0; j <= Constants.CURR; j++) mkv.getDyspregcost()[j][i][t] = 0.0;		//cost of day open
				for(j = 0; j <= Constants.CURR; j++) mkv.getDyspregcost_cnt()[j][i][t] = 0.0;	//cost of day open counter
			}
			if(t == 0)
			{
				for(i = 0; i <= Constants.MIM; i++) for(j = 0; j <= 2; j++) mkv.getHz_open()[i][j] = 0.0;
				for(i = 0; i <= Constants.MIM; i++) for(j = 0; j <= 2; j++) mkv.getHz_preg()[i][j] = 0.0;
				for(k = 0; k <= Constants.CURR; k++) for(l = 0; l <= Constants.LACT; l++) for(i = 0; i <= Constants.MIM; i++) for(j = 0; j <= Constants.MPREG; j++) mkv.getPind_rpo()[k][l][i][j] = 0.0;
				for(k = 0; k <= Constants.CURR; k++) for(l = 0; l <= Constants.LACT; l++) for(i = 0; i <= Constants.MIM; i++) for(j = 0; j <= Constants.MPREG; j++) mkv.getPind_rpo_cnt()[k][l][i][j] = 0.0;
			}
	
			if((int)t % (int)((double)maxstages / 5) == 0 || t == 1 || t == maxstages)
			{
				System.out.println(50+(double)t/(double)maxstages*50);
				Utility.write_msg_file(20,50+(double)t/(double)maxstages*50);
			}
	
		}//end t
	
		for(i = 0; i <= Constants.MIM; i++) mkv.getStatus()[i][1] = mkv.getStatus()[i][0] = 0.0;
		for(i = 0; i <= Constants.MIM; i++) mkv.getCulled()[i] = 0.0;
		for(i = 0; i <= Constants.MIM; i++) mkv.getHz_cull()[i] = 0.0;
		for(k = 0; k <= Constants.CURR; k++) for(j = 0; j <= Constants.MPREG+1; j++) mkv.getPv_curr()[k][j] = mkv.getPv_curr_cnt()[k][j] = 0.0;
		for(k = 0; k <= Constants.LACT; k++) for(j = 0; j <= Constants.MPREG+1; j++) mkv.getPv_lact()[k][j] = mkv.getPv_lact_cnt()[k][j] = 0.0;
		for(k = 0; k <= Constants.MIM; k++)  for(j = 0; j <= Constants.MPREG+1; j++) mkv.getPv_mim()[k][j] = mkv.getPv_mim_cnt()[k][j] = 0.0;
		for(j = 0; j <= Constants.MPREG+1; j++) mkv.getPv_mpreg()[j] = mkv.getPv_mpreg_cnt()[j] = 0.0;
		for(k = 0; k <= Constants.CURR; k++) for(i = 0; i <= Constants.LACT; i++) for(j = 0; j <= Constants.MPREG+1; j++) 
			mkv.getPv_curr_lact()[k][i][j] = mkv.getPv_curr_lact_cnt()[k][i][j] = 0.0;
	
	//&
		for(l = 0; l <= Constants.LACT; l++)
			for(j = 0; j <= Constants.SEA; j++)  
			{
				mkv.getDaysvwp_clv()[l][j] = 0.0;
				mkv.getDaysfs_clv()[l][j] = 0.0;
				mkv.getDayslbm_clv()[l][j] = 0.0;
				mkv.getDaystoconception_clv()[l][j] = 0.0;
				mkv.getCalvinginterval_clv()[l][j] = 0.0;
			}
	
		//start year
		for(i = 0; i <= Constants.YEAR; i++)
		{
			mkv.getAbortrisk()[i] = 0.0;
			mkv.getAnnuity_cyr()[i] = 0.0;
			mkv.getHerdlife()[i] = 0.0;
			mkv.getCash_cyr()[i] = 0.0;
			mkv.getCash_cwt()[i] = 0.0;
			mkv.getCowvalue()[i] = 0.0;
			mkv.getMilkyield_cyr()[i] = 0.0;
			mkv.getMilkyield_cwt()[i] = 0.0;
			mkv.getMilksales_cyr()[i] = 0.0;
			mkv.getMilksales_cwt()[i] = 0.0;
			mkv.getBreedingcost_cwt()[i] = 0.0;
			mkv.getBreedingcost_cyr()[i] = 0.0;
			mkv.getBreedingsupplycost_cyr()[i] = 0.0;
			mkv.getBreedingsupplycost_cwt()[i] = 0.0;
			mkv.getPreglaborcost()[i] = 0.0;
			mkv.getPregsupplycost()[i] = 0.0;
			mkv.getFeedcost_cyr()[i] = 0.0;
			mkv.getFeedcost_cwt()[i] = 0.0;
			mkv.getFeedcostdry_cyr()[i] = 0.0;
			mkv.getFeedcostlac_cyr()[i] = 0.0;
			mkv.getHeifercost_cyr()[i] = 0.0;
			mkv.getHeifercost_cwt()[i] = 0.0;
			mkv.getCalfsales_cyr()[i] = 0.0;
			mkv.getCalfsales_cwt()[i] = 0.0;
			mkv.getCowsales_cyr()[i] = 0.0;
			mkv.getCowsales_cwt()[i] = 0.0;
			mkv.getDayspreg_cost()[i] = 0.0;
			mkv.getDaysopen_cost()[i] = 0.0;
			mkv.getEaa_cyr()[i] = 0.0;
			mkv.getEaa_cwt()[i] = 0.0;
			mkv.getLivestockcost_cyr()[i] = 0.0;
			mkv.getLivestockcost_cwt()[i] = 0.0;
			mkv.getLaborcost_cyr()[i] = 0.0;
			mkv.getLoss_cwt()[i] = 0.0;
			mkv.getLoss_cyr()[i] = 0.0;
			mkv.getLaborcost_cwt()[i] = 0.0;
			mkv.getOthercost_cwt()[i] = 0.0;
			mkv.getOthercost_cyr()[i] = 0.0;
			mkv.getFixedcost_labor_cwt()[i] = 0.0;
			mkv.getFixedcost_labor_cyr()[i] = 0.0;
			mkv.getFixedcost_other_cwt()[i] = 0.0;
			mkv.getFixedcost_other_cyr()[i] = 0.0;
			
			mkv.getVetcost_cyr()[i] = 0.0;
			mkv.getVetcost_cwt()[i] = 0.0;
	
			
			mkv.getCullrateall()[i] = 0.0;
			mkv.getCullratevol()[i] = 0.0;
			mkv.getCullrateinv()[i] = 0.0;
	//		mkv.getvwpy[i] = 0.0;
			mkv.getCullpricey()[i] = 0.0;
			mkv.getPv_open()[i] = 0.0;
			mkv.getPv_preg()[i] = 0.0;
			mkv.getOpendays()[i] = 0.0;
	
			for(j = 0; j <= Constants.SEA; j++)
			{
				mkv.getFracrepl_clv()[j][i] = 0.0;
				mkv.getFracpind_clv()[j][i] = 0.0;
				mkv.getRepl_clv()[j][i] = 0.0;
				mkv.getPind_clv()[j][i] = 0.0;
				mkv.getDaysopen()[j][i] = 0.0;
				mkv.getPregnancyrate0()[j][i] = 0.0;
				mkv.getPregnancyrate1()[j][i] = 0.0;
			}
	
			for(j = 0; j <= maxstages; j++)
				for(k = 0; k <= Constants.MIM; k++)
				{
					mkv.getReplopen_cur()[k][j][i] = 0.0;
					mkv.getReplopen_dim()[k][j][i] = 0.0;
					mkv.getReplopen_cnt()[k][j][i] = 0.0;
					mkv.getReplopen_iof()[k][j][i] = 0.0;
					mkv.getReplopen_mlk()[k][j][i] = 0.0;
					mkv.getReplopen_rev()[k][j][i] = 0.0;
					mkv.getReplopen_rpo()[k][j][i] = 0.0;
				}
			for(k = 0; k <= Constants.MIM; k++)
				for(j = 0; j <= maxstages; j++)	
					mkv.getInseminationvalue()[k][j][i] = 0.0;
	
		}//end year
	
		if(what == 3) System.out.println("\n RESET_MKV_RECORD done ...");
		return mkv;
	}/*** end RESET_MKV_RECORD ***/
	
	
	
	/******************************************************************************
	RESET_POL_RECORD: Policy record: 0 = replace, 1 = keep, 2 = serv.
	******************************************************************************/
	public static PolRecord reset_pol_record(PolRecord pol)
	{
		int	i = 0, j = 0, k = 0, m = 0, n = 0;
	
	//	System.out.println("\n RESET_POL_RECORD started ...");
		for(i = 0; i <= Constants.CURR; i++)
			for(j = 0; j <= Constants.LACT; j++)
				for(k = 0; k <= Constants.MIM; k++)
					for(m = 0; m <= Constants.MPREG; m++)
						for(n = 0; n <= 2*Constants.SEA; n++)
						{
	//System.out.println("\n i:%d j:%d k:%d m:%d",i,j,k,m);
							pol.getCow()[i][j][k][m][n] = Constants.POL;
						}
		for(n = 0; n <= 2*Constants.SEA; n++) pol.getEnter()[n] = Constants.POL;
	
	//	System.out.println("\n RESET_POL_RECORD done ...");
		return pol;
	}/*** end RESET_POL_RECORD ***/
	
	
	
	/******************************************************************************
	RESET_RPO_RECORD:
	******************************************************************************/
	public static RpoRecord reset_rpo_record(RpoRecord rpo)
	{
		int	i = 0, j = 0, k = 0, m = 0, n = 0;
	
		System.out.println("\n RESET_RPO_RECORD started ...");
		for(i = 0; i <= Constants.CURR; i++)
			for(j = 0; j <= Constants.LACT; j++)
				for(k = 0; k <= Constants.MIM; k++)
					for(m = 0; m <= Constants.MPREG; m++)
						for(n = 0; n <= 2*Constants.SEA; n++)
						{
							rpo.getCow()[i][j][k][m][n] = -Constants.X;
							rpo.getPrg()[i][j][k][m][n] = -Constants.X;	//0.0
							rpo.getRpl()[i][j][k][m][n] = 0.0;
						}
		System.out.println("\n RESET_RPO_RECORD done ...");
		return rpo;
	}/*** end RESET_RPO_RECORD ***/
	
	

	
	
	

}